package user

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-mp-api/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-mp-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/base"
	"git.myscrm.cn/golang/stark/v4"
)

type userNewRepository struct {
}

func NewUserNewRepository() UserNewRepositoryIface {
	return &userNewRepository{}
}

func (r *userNewRepository) SyncUsers(ctx context.Context, request *pb.BaseOldUserSyncRequest) (*pb.BaseOldUserSyncResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewBaseUserServiceClient(conn)
	res, err := client.SyncUserInfo(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "SyncUserInfo-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}

func (r *userNewRepository) GetAllUserList(ctx context.Context) (*pb.GetAllUserListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBaseUserServiceClient(conn)
	res, err := client.GetAllUserList(ctx, &pb.EmptyRequest{})
	if err != nil {
		stark.Logger.Errorf(ctx, "GetAllUserList err:%s", err.Error())
		return nil, err
	}

	return res, nil
}

func (r *userNewRepository) GetAllUserWithOrganization(ctx context.Context) (*pb.GetAllUserWithOrganizationResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewBaseUserServiceClient(conn)
	res, err := client.GetAllUserWithOrganization(ctx, &pb.GetAllUserWithOrganizationRequest{})
	if err != nil {
		stark.Logger.Errorf(ctx, "GetAllUserWithOrganization err:%s", err.Error())
		return nil, err
	}

	return res, nil
}

func (r *userNewRepository) GetUserIdUserUid(ctx context.Context, request *pb.GetUserIdUserUidRequest) (*pb.GetUserIdUserUidResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewBaseUserServiceClient(conn)
	res, err := client.GetUserIdUserUid(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetUserIdUserUid-error: %+v, request: %+v", request, err)
		return nil, err
	}
	return res, nil
}
